//
//  ReportProjectListVC.m
//  efairy
//
//  Created by 李锋 on 2019/3/11.
//  Copyright © 2019年 East. All rights reserved.
//

#import "ReportProjectListVC.h"
#import <MJRefresh.h>
#import "XDAFNetworking.h"
#import "ApiManager.h"
#import "GlobalHeader.h"
#import "EFAModel.h"
#import "AreaAddressModel.h"
#import "YYModel.h"
#import "AddressCell.h"
#import "ReportProjectListCell.h"
#import "ReportProjectModel.h"
#import "ReportBuildVC.h"

@interface ReportProjectListVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic, strong) UITableView *tableView;//要生成报表的项目列表
@property (nonatomic, strong) UITableView *addressTableView;
@property (nonatomic, strong) UIView *checkAddress;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UITextField *searchText;
@property (nonatomic, strong) UIView *chooseView;
@property (nonatomic, strong) UIImageView *allChooseImage;
@property (nonatomic, strong) UILabel *chooseRowHintLabel;
@property (nonatomic, strong) UIButton *submit;
@property (nonatomic, assign) int pageIndex;
@property (nonatomic, strong) MJRefreshComponent *refreshViewComponent;
@property (nonatomic, strong) UIView *addressChooseView;
@property (nonatomic, strong) NSMutableArray<ReportProjectListModel *> *projectArray;
@property (nonatomic, strong) NSMutableSet<ReportProjectListModel *> *chooseProject;
@property (nonatomic, strong) NSMutableArray<ProvinceModel *> *provinceArray;//省
@property (nonatomic, strong) NSMutableArray<CityModel *> *cityArray;//市
@property (nonatomic, strong) NSMutableArray<DistrictModel *> *districtArray;//县
@property (nonatomic, strong) NSMutableArray<TownshipModel *> *townshipArray;//区/街道
@property (nonatomic, strong) NSString *provinceId;//省id
@property (nonatomic, strong) NSString *cityId;//市
@property (nonatomic, strong) NSString *districtId;//县
@property (nonatomic, strong) NSString *townshipId;//区
@property (nonatomic, assign) int provinceArrayIndex;//选中省的第几个item
@property (nonatomic, assign) int cityArrayIndex;//选中市的第几个item
@property (nonatomic, assign) int districtArrayIndex;//选中区县的第几个item
@property (nonatomic, assign) int townshipArrayIndex;//选中街道的第几个item
@property (nonatomic, assign) int selectWhichAddress;//选择哪一个地址 0 省 1市 2 区县 3街道



//地址选择栏
@property (nonatomic,strong) UIView  *addressTopView;//地址选择页面的 透明部分view用于点击透明部分关闭view
@property (nonatomic,strong) UIView  *addressView;//地址选择部分的view
@property (nonatomic,strong) UILabel *confirm;//确认
@property (nonatomic,strong) UILabel *cancel;//取消
@property (nonatomic,strong) UILabel *provincen;//省title
@property (nonatomic,strong) UILabel *city;//市title
@property (nonatomic,strong) UILabel *district;//区县title
@property (nonatomic,strong) UILabel *township;//街道title
@property (nonatomic,strong) UIView *provincenLine;//省title 下划线
@property (nonatomic,strong) UIView *cityLine;//市title 下划线
@property (nonatomic,strong) UIView *districtLine;//区县title 下划线
@property (nonatomic,strong) UIView *townshipLine;//街道title 下划线
@property (nonatomic,strong) UIView *contentTableView;//tableview显示区域
@property (nonatomic,assign) BOOL isChooseAll;

@end

@implementation ReportProjectListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _chooseProject = [[NSMutableSet alloc]init];
    _isChooseAll = NO;
    [self setNavigationBar];
    [self initView];
    [self initAddressViews];
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
//    self.view.frame = self.view.bounds;
}


- (BOOL)hidesBottomBarWhenPushed{
    return YES;
}


-(void)setNavigationBar{
    [self.navigationController setNavigationBarHidden:YES];
}



-(void)initView{
    float barHeight = self.navigationController.navigationBar.frame.size.height;
    _checkAddress = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    _checkAddress.backgroundColor = [UIColor whiteColor];
    
    UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickAddressView:)];
    [_checkAddress addGestureRecognizer:tapGesturRecognizer];
    
    
    
    _addressLabel = [[UILabel alloc]init];
    _addressLabel.text=@"选择地区";
    _addressLabel.textColor=ColorFromRGB(0x333333);
    _addressLabel.font= [UIFont systemFontOfSize:15.0];
    [_checkAddress addSubview:_addressLabel];
    
    
    //右箭头图标
    UIImageView *rightArrow=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 8, 13)];
    rightArrow.image=[UIImage imageNamed:@"icon_right_arrow"];
    [_checkAddress addSubview:rightArrow];
    
    _addressLabel.sd_layout
    .heightIs(15)
    .leftSpaceToView(_checkAddress, 16)
    .rightSpaceToView(_checkAddress, 16)
    .topSpaceToView(_checkAddress, 12.5);
    
    rightArrow.sd_layout.heightIs(13).widthIs(8)
    .topSpaceToView(_checkAddress, 13.5)
    .rightSpaceToView(_checkAddress, 16);
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 1)];
    line.backgroundColor = ColorFromRGB(0xe5e5e5);
    
    UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 41, SCREEN_WIDTH, 40)];
    //搜索栏背景
    UIView *searchBg =  [[UIView alloc]init];
    searchBg.layer.borderWidth=0.0f;
    searchBg.layer.cornerRadius=16.0f;
    searchBg.backgroundColor = ColorFromRGB(0xefefef);
    [searchView addSubview:searchBg];
    
    //创建左侧搜索图标
    UIImageView *searchIcon=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];
    searchIcon.image=[UIImage imageNamed:@"search_icon"];
    [searchView addSubview:searchIcon];
    
    //文本输入框
    _searchText = [[UITextField alloc]init];
    _searchText.backgroundColor = ColorFromRGB(0xefefef);
    _searchText.clearButtonMode=UITextFieldViewModeAlways;//只要输入框有内容就出现
    // 设置提示文字
    _searchText.placeholder = @"请输项目名称";
    _searchText.layer.borderWidth=0.0f;
    _searchText.layer.cornerRadius=16.0f;
    _searchText.returnKeyType = UIReturnKeySearch;//变为搜索按钮
    _searchText.delegate = self;//设置代理
    [searchView addSubview:_searchText];
    
    searchBg.sd_layout.heightIs(30)
    .topSpaceToView(searchView, 5)
    .leftSpaceToView(searchView, 16)
    .rightSpaceToView(searchView, 16);
    
    searchIcon.sd_layout.heightIs(15)
    .widthIs(15)
    .topSpaceToView(searchView, 12.5)
    .leftSpaceToView(searchView, 32);
    
    _searchText.sd_layout.heightIs(30)
    .topSpaceToView(searchView, 5)
    .leftSpaceToView(searchIcon, 12)
    .rightSpaceToView(searchView, 16);
    
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0, 80, SCREEN_WIDTH, 1)];
    line2.backgroundColor = ColorFromRGB(0xeff0f4);
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 81, SCREEN_WIDTH, SCREEN_HEIGHT-barHeight-statusBarHeight-40-176)];
    self.tableView.backgroundColor = ColorFromRGB(0xeff0f4);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    __weak typeof(self) weakSelf = self;
    //..下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.refreshViewComponent = weakSelf.tableView.mj_header;
        weakSelf.pageIndex = 1;
        [weakSelf queryProjectList:YES];
    }];
    
    
    //..上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.refreshViewComponent = weakSelf.tableView.mj_footer;
        weakSelf.pageIndex = weakSelf.pageIndex + 1;
        [weakSelf queryProjectList:NO];
    }];
    
    
    //Y的坐标必须以父controller 中给定的高度为基准开始算 父controller 给定的高度是：SCREEN_HEIGHT-barHeight-statusBarHeight-40
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT-barHeight-statusBarHeight-40-95, SCREEN_WIDTH, 50)];
    bottomView.backgroundColor = [UIColor whiteColor];
    

    _chooseView = [[UIView alloc]init];
    UITapGestureRecognizer *chooseTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickChooseView:)];
    [_chooseView addGestureRecognizer:chooseTap];
    
    
    
    _allChooseImage = [[UIImageView alloc]init];
    _allChooseImage.image = [UIImage imageNamed:@"icon_no_checked"];
    _chooseRowHintLabel = [[UILabel alloc]init];
    _chooseRowHintLabel.text = @"全选(已选0条)";
    _chooseRowHintLabel.textColor = ColorFromRGB(0x333333);
    _chooseRowHintLabel.font = [UIFont systemFontOfSize:12.f];
    
    [_chooseView sd_addSubviews:@[_allChooseImage,_chooseRowHintLabel]];
    _allChooseImage.sd_layout.widthIs(20).heightIs(20).leftSpaceToView(_chooseView, 15).topSpaceToView(_chooseView, 15);
    _chooseRowHintLabel.sd_layout.widthIs(180).heightIs(12).leftSpaceToView(_allChooseImage, 12).topSpaceToView(_chooseView, 19);
    
    _submit = [[UIButton alloc]init];
    [_submit setTitle:@"下一步" forState:UIControlStateNormal];
    [_submit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_submit setBackgroundColor:ColorFromRGB(0x282449)];
    _submit.titleLabel.font = [UIFont systemFontOfSize:16.f];
    [_submit.layer setCornerRadius:16];
    [_submit addTarget:self action:@selector(goNextVC) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *line3 = [[UIView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT-barHeight-statusBarHeight-40-95, SCREEN_WIDTH, 0.5)];
    line3.backgroundColor = ColorFromRGB(0xe5e5e5);
    
    [bottomView sd_addSubviews:@[_chooseView,_submit]];
    _chooseView.sd_layout.widthIs(230).heightIs(50).leftSpaceToView(bottomView, 0).topSpaceToView(bottomView, 0);
    _submit.sd_layout.widthIs(74).heightIs(32).rightSpaceToView(bottomView, 9).topSpaceToView(bottomView, 9);
    
    
    [self.view sd_addSubviews:@[_checkAddress,line,searchView,line2,_tableView,bottomView,line3]];
    
}


-(void)onClickChooseView:(id)tap{
    if(_isChooseAll){
        _isChooseAll = NO;
        _allChooseImage.image = [UIImage imageNamed:@"icon_no_checked"];
        for(ReportProjectListModel *item in _projectArray){
            item.isChecked = NO;
            [_chooseProject removeObject:item];
            
        }
        [self setNoAllChooseHint];
    }else{
         _isChooseAll = YES;
         _allChooseImage.image = [UIImage imageNamed:@"icon_checked"];
        for(ReportProjectListModel *item in _projectArray){
            item.isChecked = YES;
            [_chooseProject addObject:item];
        }
        [self setAllChooseHint];
    }
    
    [self.tableView reloadData];
}

//全选后 提示文字
-(void)setAllChooseHint{
     _chooseRowHintLabel.text = [NSString stringWithFormat:@"全不选(已选%lu条)",_chooseProject.count];
}

//未全选的提示文字
-(void)setNoAllChooseHint{
    _chooseRowHintLabel.text = [NSString stringWithFormat:@"全选(已选%lu条)",_chooseProject.count];
}

//下一步，进入报表生成页面
-(void)goNextVC{
    ReportBuildVC *vc = [[ReportBuildVC alloc]init];
    vc.projectArray = _chooseProject.allObjects;
    [self showViewController:vc sender:nil];
    
}




#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if([tableView isEqual:self.tableView]){
        if (_projectArray!=nil) {
            if (_projectArray.count>0) {
                return _projectArray.count;
            }
        }
        return  0;
    }else{
        return 1;
    }
}


//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if([tableView isEqual:self.tableView]){
        return 1;
    }else if([tableView isEqual:self.addressTableView]){
        switch (_selectWhichAddress) {
            case 0:
                if(_provinceArray != nil && _provinceArray.count > 0){
                    return _provinceArray.count;
                }
                break;
            case 1:
                if(_cityArray != nil && _cityArray.count > 0){
                    return _cityArray.count;
                }
                break;
            case 2:
                if(_districtArray != nil && _districtArray.count > 0){
                    return _districtArray.count;
                }
                break;
            case 3:
                if(_townshipArray != nil && _townshipArray.count > 0){
                    return _townshipArray.count;
                }
                break;
            default:
                break;
        }
        
    }
    return 0;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return NO;
}


//设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if([tableView isEqual:self.tableView]){
        ReportProjectListCell *cell = [tableView dequeueReusableCellWithIdentifier:[@"ReportProjectListCell" description]];
        if(!cell){
            cell = [[ReportProjectListCell alloc]init];
            cell.backgroundColor = [UIColor whiteColor];
        }
        if(_projectArray){
             [cell setDataModel:_projectArray[indexPath.section]];
        }
        // 此步设置用于实现cell的frame缓存，可以让tableview滑动更加流畅
        [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
        return cell;
    }else{
        AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:[@"AddressCell" description]];
        if(!cell){
            cell = [[AddressCell alloc]init];
        }
        cell.backgroundColor = [UIColor whiteColor];
        switch (_selectWhichAddress) {
            case 0:
                [cell configViewWithItem:_provinceArray[indexPath.row]];
                if(_provinceArrayIndex == indexPath.row){
                    [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
                }
                break;
            case 1:
                [cell configViewWithItem:_cityArray[indexPath.row]];
                if(_cityArrayIndex == indexPath.row){
                    [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
                }
                break;
            case 2:
                [cell configViewWithItem:_districtArray[indexPath.row]];
                if(_districtArrayIndex == indexPath.row){
                    [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
                }
                break;
            case 3:
                [cell configViewWithItem:_townshipArray[indexPath.row]];
                if(_townshipArrayIndex == indexPath.row){
                    [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
                }
                break;
            default:
                break;
        }
        // 此步设置用于实现cell的frame缓存，可以让tableview滑动更加流畅
        [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
        
        return cell;
    }
    
}



#pragma mark - UITableViewDelegate

//cell高度设置s
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView cellHeightForIndexPath:indexPath cellContentViewWidth:SCREEN_WIDTH tableView:tableView];
}

//设置分割线
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if([tableView isEqual:self.tableView]){
         return 8.f;
    }
    return 0.0001;
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] init];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]init];
}

//点击cell执行该方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if([tableView isEqual:self.tableView]){
        if(_projectArray.count > 0 && indexPath.section < _projectArray.count){
           
                if(_projectArray[indexPath.section].isChecked){
                    _projectArray[indexPath.section].isChecked = NO;
                    [_chooseProject removeObject:_projectArray[indexPath.section]];
                     if(_isChooseAll){
                        _isChooseAll = NO;
                        _allChooseImage.image = [UIImage imageNamed:@"icon_no_checked"];
                     }
                    [self setNoAllChooseHint];
                }else{
                    _projectArray[indexPath.section].isChecked  = YES;
                    [_chooseProject addObject:_projectArray[indexPath.section]];
                    if(_chooseProject.count == _projectArray.count){
                        _isChooseAll= YES;
                        _allChooseImage.image = [UIImage imageNamed:@"icon_checked"];
                        [self setAllChooseHint];
                    }else{
                         [self setNoAllChooseHint];
                    }
                   
                }
            [self.tableView reloadData];
           
        }
    }else if([tableView isEqual:self.addressTableView]){
        switch (_selectWhichAddress) {
            case 0:
                if(_provinceArrayIndex != (int)indexPath.row){//下标相同时表示已经为选中的数据，没不要在往下执行了
                    _provinceArrayIndex = (int)indexPath.row;//记录选择的下标，以便下次显示时知道是那条数据为选中的
                    if(_cityArrayIndex > -1){
                        _cityArrayIndex = -1;//重置为-1就是为了让其不是选中状态
                    }
                    if(_districtArrayIndex > -1){
                        _districtArrayIndex =-1;
                    }
                    if(_townshipArrayIndex > -1){
                        _townshipArrayIndex = -1;
                    }
                    _provincen.text = _provinceArray[indexPath.row].efairyprovince_name;
                    _city.text = @"请选择市";
                    _district.text = @"请选择区县";
                    _township.text = @"请选择街道";
                    _provinceId = _provinceArray[indexPath.row].efairyprovince_id;
                    _cityId = @"";
                    _districtId = @"";
                    _townshipId=@"";
                    _cityArray = _provinceArray[indexPath.row].city_list;
                    [_districtArray removeAllObjects];
                    [_townshipArray removeAllObjects];
                }
                break;
            case 1:
                if(_cityArrayIndex != (int)indexPath.row){
                    _cityArrayIndex= (int)indexPath.row;
                    if(_districtArrayIndex > -1){
                        _districtArrayIndex =-1;
                    }
                    if(_townshipArrayIndex > -1){
                        _townshipArrayIndex = -1;
                    }
                    _city.text = _cityArray[indexPath.row].efairycity_name;
                    _district.text = @"请选择区县";
                    _township.text = @"请选择街道";
                    _cityId =_cityArray[indexPath.row].efairycity_id;
                    _districtId = @"";
                    _townshipId=@"";
                    _districtArray = _cityArray[indexPath.row].district_list;
                    [_townshipArray removeAllObjects];
                }
                
                break;
                
            case 2:
                if(_districtArrayIndex != (int)indexPath.row){
                    _districtArrayIndex = (int)indexPath.row;
                    if(_townshipArrayIndex > -1){
                        _townshipArrayIndex = -1;
                    }
                    _district.text = _districtArray[indexPath.row].efairydistrict_name;
                    _township.text = @"请选择街道";
                    _districtId = _districtArray[indexPath.row].efairydistrict_id;
                    _townshipId=@"";
                    _townshipArray = _districtArray[indexPath.row].township_list;
                }
                
                break;
            case 3:
                if(_townshipArrayIndex !=(int)indexPath.row){
                    _township.text = _townshipArray[indexPath.row].efairytownship_name;
                    _townshipId =_townshipArray[indexPath.row].efairytownship_id;
                    _townshipArrayIndex = (int)indexPath.row;
                }
                break;
                
            default:
                break;
        }
    }

}

//停止刷新头部或者底部
- (void)tableViewEndRefresh:(BOOL)isHeader {
    NSLog(@"停止刷新是否是头部---%zd", isHeader);
    if (isHeader) {
        [self.tableView.mj_header endRefreshing];
    } else {
        [self.tableView.mj_footer endRefreshing];
    }
}

-(void)queryProjectList:(BOOL)isHeader{
    // 发布Cos需要登录
    if (![EFAModel sharedInstance].isLogin) {
        return;
    }
    
    NSMutableDictionary *address = [NSMutableDictionary dictionary];
    
    if(_provinceId && _provinceId.length > 0){
        [address setObject:_provinceId forKey:@"efairyprovince_id"];// 省份
    }
    if(_cityId && _cityId.length > 0){
        [address setObject:_cityId forKey:@"efairycity_id"];// 城市
    }
    if(_districtId && _districtId.length > 0){
        [address setObject:_districtId forKey:@"efairydistrict_id"];// 区
    }
    if(_townshipId && _townshipId.length > 0){
        [address setObject:_townshipId forKey:@"efairytownship_id"];// 街道
    }
    NSDictionary *params =  @{@"efairyuser_id" : @([EFAModel sharedInstance].loginInfo.user_info.efairyuser_id),
                                  @"access_token" : [EFAModel sharedInstance].loginInfo.access_token,
                                  @"regeo_info":[address yy_modelToJSONString],
                                  @"keyword":@"",
                                  @"page":@(_pageIndex),
                                  @"size":@"5"
                                  };
    
 

    NSLog(@"报表项目列表请求参数：%@",params);
    [[XDAFNetworking sharedInstance]GET:[ApiManager apiReportProjectList] parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task,id _Nullable responseObject){
         NSLog(@"获取可生成报表的项目列表%@",responseObject);
        [self tableViewEndRefresh:isHeader];
        ReportProjectModel *reportModel=  [ReportProjectModel yy_modelWithDictionary:responseObject[EFAGlobalJsonKeyResult]];
        if(reportModel && reportModel.project_list){
            if(isHeader){
                [_chooseProject removeAllObjects];
                _allChooseImage.image = [UIImage imageNamed:@"icon_no_checked"];
                _isChooseAll = NO;
                [self setNoAllChooseHint];
                _projectArray = reportModel.project_list;
        
            }else{
                [_projectArray addObjectsFromArray:reportModel.project_list];
                if(_isChooseAll){
                    _isChooseAll = NO;
                    _allChooseImage.image = [UIImage imageNamed:@"icon_no_checked"];
                    [self setNoAllChooseHint];
                }
            }
            if(_projectArray.count >= reportModel.total_rows){
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer resetNoMoreData];
            }
        }
        [self.tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask *_Nullable task,NSError * _Nullable error){
        NSLog(@"错误信息：%@",error);
        NSString *msg = [[ApiErrorCode sharedInstance] errorCodeMsgWithError:error];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:msg];
    }];
}

-(void)requestAddress {
    // 发布Cos需要登录
    if (![EFAModel sharedInstance].isLogin) {
        
        return;
    }
    NSDictionary *params =  @{@"efairyuser_id" : @([EFAModel sharedInstance].loginInfo.user_info.efairyuser_id),
                              @"access_token" : [EFAModel sharedInstance].loginInfo.access_token};
    [[XDAFNetworking sharedInstance]GET:[ApiManager apiReportGeoInfoList] parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task,id _Nullable responseObject){
        //         NSLog(@"区域地址11：%@",responseObject[EFAGlobalJsonKeyResult]);
        
        ProvinceListModel *provinceModel=  [ProvinceListModel yy_modelWithDictionary:responseObject[EFAGlobalJsonKeyResult]];
        _provinceArray =provinceModel.province_list;
        _provinceArrayIndex = 0;
        _cityArrayIndex = -1;
        _districtArrayIndex = -1;
        _townshipArrayIndex = -1;
        _selectWhichAddress = 0;
        if(_provinceArray.count > 0){
            //默认选择第一个省查询
            _addressLabel.text =_provinceArray[0].efairyprovince_name;//设置选择栏文字
            _provincen.text =_provinceArray[0].efairyprovince_name;//设置地址列表省份选择title文字
            _cityArray  = _provinceArray[0].city_list;//赋值默认查询省下面城市列表信息
            _provinceId =_provinceArray[0].efairyprovince_id;//默认省id
        }
        
        // 马上进入刷新状态 查询项目列表
        [self.tableView.mj_header beginRefreshing];
//        [self.addressTableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask *_Nullable task,NSError * _Nullable error){
        NSString *msg = [[ApiErrorCode sharedInstance] errorCodeMsgWithError:error];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:msg];
    }];
    
}



//隐藏地址选择页面
-(void)isHiddenAddressView{
    if(_addressChooseView.isHidden){
        _addressChooseView.hidden=NO;
        [self.addressTableView reloadData];
    }else{
        _addressChooseView.hidden=YES;
    }
}

//点击地址选择入口栏
-(void)onClickAddressView:(id)tap{
    if(_provinceArray == nil  || _provinceArray.count == 0){
        [self requestAddress];
    }
    [self isHiddenAddressView];
}

//点击地址选择页面的透明部分
-(void)onClickAddressChooseView:(id)tap{
    [self isHiddenAddressView];
}


-(void) onClickProvinceTitle:(id)tap{
    _selectWhichAddress = 0;
    _provincen.textColor =ColorFromRGB(0x282449);
    _city.textColor = ColorFromRGB(0x666666);
    _district.textColor = ColorFromRGB(0x666666);
    _township.textColor = ColorFromRGB(0x666666);
    _provincenLine.hidden=NO;
    _cityLine.hidden= YES;
    _districtLine.hidden=YES;
    _townshipLine.hidden=YES;
    [self.addressTableView reloadData];
    
}

-(void) onClickCityTitle:(id)tap{
    _selectWhichAddress = 1;
    _provincen.textColor =ColorFromRGB(0x666666);
    _city.textColor = ColorFromRGB(0x282449);
    _district.textColor = ColorFromRGB(0x666666);
    _township.textColor = ColorFromRGB(0x666666);
    _provincenLine.hidden=YES;
    _cityLine.hidden= NO;
    _districtLine.hidden=YES;
    _townshipLine.hidden=YES;
    [self.addressTableView reloadData];
    
}

-(void) onClickDistrictTitle:(id)tap{
    _selectWhichAddress = 2;
    _provincen.textColor =ColorFromRGB(0x666666);
    _city.textColor = ColorFromRGB(0x666666);
    _district.textColor = ColorFromRGB(0x282449);
    _township.textColor = ColorFromRGB(0x666666);
    _provincenLine.hidden=YES;
    _cityLine.hidden= YES;
    _districtLine.hidden=NO;
    _townshipLine.hidden=YES;
    [self.addressTableView reloadData];
    
}

-(void) onClickTownshipTitle:(id)tap{
    _selectWhichAddress = 3;
    _provincen.textColor =ColorFromRGB(0x666666);
    _city.textColor = ColorFromRGB(0x666666);
    _district.textColor = ColorFromRGB(0x666666);
    _township.textColor = ColorFromRGB(0x282449);
    _provincenLine.hidden=YES;
    _cityLine.hidden= YES;
    _districtLine.hidden=YES;
    _townshipLine.hidden=NO;
    [self.addressTableView reloadData];
}

-(void) onClickConfirm:(id)tap{
    _addressChooseView.hidden=YES;
    self.pageIndex = 1;
    [self queryProjectList:YES];
    NSString *adressStr =@"选择地区";
    if(_provinceId != nil && _provinceId.length > 0){
        adressStr = _provincen.text;
    }
    if(_cityId != nil && _cityId.length > 0){
        adressStr = [adressStr stringByAppendingString:_city.text];
    }
    if(_districtId != nil && _districtId.length > 0){
        adressStr = [adressStr stringByAppendingString:_district.text];
    }
    
    if(_townshipId != nil && _townshipId.length > 0){
        adressStr = [adressStr stringByAppendingString:_township.text];
    }
    _addressLabel.text = adressStr;
}

-(void) onClickCancel:(id)tap{
    _addressChooseView.hidden=YES;
}


-(void) initAddressViews{
    UIWindow *wd = [[UIApplication sharedApplication].windows lastObject];
    _addressChooseView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT)];
    [wd addSubview:_addressChooseView];
    _addressChooseView.hidden=YES;
    
    
    _addressTopView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/2-96)];
    _addressTopView.backgroundColor = [UIColor blackColor];
    _addressTopView.alpha = 0.5;
    
    UITapGestureRecognizer *tapView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickAddressChooseView:)];
    [_addressTopView addGestureRecognizer:tapView];
    
    _addressView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/2-96, SCREEN_WIDTH, SCREEN_HEIGHT-(SCREEN_HEIGHT/2-96))];
    _addressView.backgroundColor = [UIColor whiteColor];
    [_addressChooseView sd_addSubviews:@[_addressTopView,_addressView,]];
    
    _confirm = [[UILabel alloc]init];
    _confirm.text=@"确定";
    _confirm.font=[UIFont systemFontOfSize:15.0f];
    _confirm.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapConfirm = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickConfirm:)];
    //    [_confirm targetForAction:@selector(onClickConfirm)tapConfirm withSender:tapConfirm];
    [_confirm addGestureRecognizer:tapConfirm];
    
    
    _cancel = [[UILabel alloc]init];
    _cancel.text=@"取消";
    _cancel.font=[UIFont systemFontOfSize:15.0f];
    _cancel.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapCancel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickCancel:)];
    [_cancel addGestureRecognizer:tapCancel];
    
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = ColorFromRGB(0xefefef);
    
    _provincen = [[UILabel alloc]init];
    _provincen.text = @"请选择省";
    _provincen.font = [UIFont systemFontOfSize:12.0f];
    _provincen.textColor = ColorFromRGB(0x666666);
    _provincen.textAlignment= NSTextAlignmentCenter;
    _provincen.userInteractionEnabled =YES;
    UITapGestureRecognizer *tapProvincen = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickProvinceTitle:)];
    [_provincen addGestureRecognizer:tapProvincen];
    
    _provincenLine = [[UIView alloc]init];
    _provincenLine.height=2;
    _provincenLine.backgroundColor=ColorFromRGB(0x282449);
    
    _city = [[UILabel alloc]init];
    _city.text = @"请选择市";
    _city.font = [UIFont systemFontOfSize:12.0f];
    _city.textColor = ColorFromRGB(0x666666);
    _city.textAlignment= NSTextAlignmentCenter;
    _city.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapCity = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickCityTitle:)];
    [_city addGestureRecognizer:tapCity];
    
    _cityLine = [[UIView alloc]init];
    _cityLine.height=2;
    _cityLine.backgroundColor=ColorFromRGB(0x282449);
    _cityLine.hidden =YES;
    
    _district = [[UILabel alloc]init];
    _district.text = @"请选择区县";
    _district.font = [UIFont systemFontOfSize:12.0f];
    _district.textColor = ColorFromRGB(0x666666);
    _district.textAlignment= NSTextAlignmentCenter;
    _district.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapDistrict = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickDistrictTitle:)];
    [_district addGestureRecognizer:tapDistrict];
    
    _districtLine = [[UIView alloc]init];
    _districtLine.height=2;
    _districtLine.backgroundColor=ColorFromRGB(0x282449);
    _districtLine.hidden =YES;
    
    _township = [[UILabel alloc]init];
    _township.text = @"请选择街道";
    _township.font = [UIFont systemFontOfSize:12.0f];
    _township.textColor = ColorFromRGB(0x666666);
    _township.textAlignment= NSTextAlignmentCenter;
    _township.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapTownship = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickTownshipTitle:)];
    [_township addGestureRecognizer:tapTownship];
    
    _townshipLine = [[UIView alloc]init];
    _townshipLine.height=2;
    _townshipLine.backgroundColor=ColorFromRGB(0x282449);
    _townshipLine.hidden =YES;
    
    UIView *lineView2 = [[UIView alloc]init];
    lineView2.backgroundColor = ColorFromRGB(0xefefef);
    
    _contentTableView = [[UIView alloc]init];
    _contentTableView.backgroundColor = [UIColor whiteColor];
    
    //    self.tableView = [UITableView new];
    //    self.tableView.backgroundColor = BG;
    //    self.tableView.delegate = self;
    //    self.tableView.dataSource = self;
    //
    //    //多余分割线不显示
    //    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    //    self.automaticallyAdjustsScrollViewInsets = NO;
    //    self.tableView.backgroundColor = [UIColor whiteColor];
    //    [self.view sd_addSubviews:@[ self.tableView ]];
    //
    
    [_addressView sd_addSubviews:@[_confirm,_cancel,lineView,_provincen,_provincenLine,_city,_cityLine,
                                   _district,_districtLine,_township,_townshipLine,lineView2,_contentTableView]];
    _cancel.sd_layout
    .widthIs(50)
    .heightIs(15)
    .leftSpaceToView(_addressView, 24)
    .topSpaceToView(_addressView, 15);
    
    _confirm.sd_layout
    .widthIs(50)
    .heightIs(15)
    .topEqualToView(_cancel)
    .rightSpaceToView(_addressView, 0);
    
    lineView.sd_layout
    .heightIs(1)
    .topSpaceToView(_confirm, 15)
    .leftSpaceToView(_addressView, 0)
    .rightSpaceToView(_addressView, 0);
    
    _provincen.sd_layout
    .widthIs((SCREEN_WIDTH-24)/4)
    .heightIs(45)
    .leftSpaceToView(_addressView, 12)
    .topSpaceToView(lineView, 0);
    
    _city.sd_layout
    .widthIs((SCREEN_WIDTH-24)/4)
    .heightIs(45)
    .leftSpaceToView(_provincen, 0)
    .topSpaceToView(lineView, 0);
    
    _district.sd_layout
    .widthIs((SCREEN_WIDTH-24)/4)
    .heightIs(45)
    .leftSpaceToView(_city,0)
    .topSpaceToView(lineView, 0);
    
    _township.sd_layout
    .widthIs((SCREEN_WIDTH-24)/4)
    .heightIs(45)
    .leftSpaceToView(_district, 0)
    .topSpaceToView(lineView, 0);
    
    
    
    _provincenLine.sd_layout
    .widthIs((SCREEN_WIDTH-24)/4)
    .heightIs(2)
    .leftSpaceToView(_addressView, 12)
    .topSpaceToView(_provincen, 0);
    
    _cityLine.sd_layout
    .widthIs((SCREEN_WIDTH-24)/4)
    .heightIs(2)
    .leftSpaceToView(_provincenLine, 0)
    .topSpaceToView(_provincen, 0);
    
    _districtLine.sd_layout
    .widthIs((SCREEN_WIDTH-24)/4)
    .heightIs(2)
    .leftSpaceToView(_cityLine, 0)
    .topSpaceToView(_provincen, 0);
    
    _townshipLine.sd_layout
    .widthIs((SCREEN_WIDTH-24)/4)
    .heightIs(2)
    .leftSpaceToView(_districtLine, 0)
    .topSpaceToView(_provincen, 0);
    
    lineView2.sd_layout
    .topSpaceToView(_provincenLine, 0)
    .heightIs(1)
    .leftSpaceToView(_addressView, 0)
    .rightSpaceToView(_addressView, 0);
    
    _contentTableView.sd_layout
    .topSpaceToView(lineView2, 0)
    .bottomSpaceToView(_addressView, 0)
    .leftSpaceToView(_addressView, 0)
    .rightSpaceToView(_addressView, 0);
    
    
    //    [_contentTableView addSubview:_addressTableView];
    self.addressTableView = [UITableView new];
    self.addressTableView.backgroundColor = [UIColor whiteColor];
    self.addressTableView.delegate = self;
    self.addressTableView.dataSource = self;
    [_contentTableView addSubview:self.addressTableView];
    
    
    self.addressTableView.sd_layout.topSpaceToView(_contentTableView, 0)
    .bottomSpaceToView(_contentTableView, 0)
    .leftSpaceToView(_contentTableView, 0)
    .rightSpaceToView(_contentTableView, 0);
    
    
    
    [self requestAddress];
}

@end
